#include<bits/stdc++.h>
#define int long long 
using namespace std;
const int N = 1.5e5+5;
int n;
pair<int,int> son[N][2];
vector<pair<int,int>> f[N];
void dfs(int u,int lim)
{
	f[u].clear();
	if(!son[u][0].first)
	{
		f[u].push_back({0,0});
		return;
	}
	int ls = son[u][0].first,wl = son[u][0].second;
	int rs = son[u][1].first,wr = son[u][1].second;
	dfs(ls,lim),dfs(rs,lim);
	int p = 0,mn = 2e18;
	for(auto i:f[ls])
	{
		while(p<f[rs].size()&&f[rs][p].first+i.second+wl+wr<=lim)
		{
			mn = f[rs][p].second;
			p++;
		}
		if(p) f[u].push_back({i.first+wl,mn+wr});
	}
	p = 0,mn = 2e18;
	for(auto i:f[rs])
	{
		while(p<f[ls].size()&&f[ls][p].first+i.second+wl+wr<=lim)
		{
			mn = f[ls][p].second;
			p++;
		}
		if(p) f[u].push_back({i.first+wr,mn+wl});
	}
	sort(f[u].begin(),f[u].end());
	vector<pair<int,int>> tmp;
	int las = 2e18;
	for(auto i:f[u])
		if(i.second<las) las = i.second,tmp.push_back(i);
	f[u] = tmp;
}
inline bool ok(int x)
{
	dfs(1,x);
	return f[1].size();
}
signed main()
{
	freopen("trip.in","r",stdin);
	freopen("trip.out","w",stdout);
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n;
	for(int i = 2,x,y;i<=n;i++)
	{
		cin>>x>>y;
		if(!son[x][0].first) son[x][0] = {i,y};
		else son[x][1] = {i,y};
	}
	int l = 1,r = 5e10,res = -1;
	while(l<=r)
	{
		int mid = (l+r)/2;
		if(ok(mid)) r = mid-1,res = mid;
		else l = mid+1;
	}
	cout<<res;
	return 0;
}
